package com.yng.simbot01.ws;

import com.yng.simbot01.service.ChatEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URI;

@Configuration
@Slf4j
public class MyWSClient {

    @Bean
    @ConditionalOnMissingBean
    public WebSocket connect() {
        WebSocket webSocketClient = null;
        try {
            URI serverUri = new URI("ws://120.55.195.74:3000/chat/123/"); // 替换为 WebSocket 服务的 URI
            webSocketClient = new WebSocket(serverUri);
            webSocketClient.connect(); // 在项目启动时自动连接 WebSocket
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("连接成功");
        // 创建并注册监听器
        ChatEventService handler = new ChatEventService();
        webSocketClient.addMessageListener(handler);
        return webSocketClient;
    }



}
